﻿'
' Boogaloo® - http://www.mindfly.com
' Copyright (c) 2008
' by Mindfly, Inc.
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
' INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
' DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
' SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
' SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
' WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
' THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Imports Boogaloo.Core
Imports Boogaloo.Core.UI
Imports Boogaloo.Core.BLL

Namespace UI.UserControls


    Partial Public Class Host_navigation
        Inherits CoreControlBase

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            hyp_websiteinfo.NavigateUrl = String.Format("{0}WebsiteInformation.aspx", Groove.path_admin_host)            
            hyp_masterpages.NavigateUrl = String.Format("{0}MasterPages.aspx", Groove.path_admin_host)
            hyp_masterpageeditor.NavigateUrl = String.Format("{0}MasterPageEditor.aspx", Groove.path_admin_host)
            hyp_themes.NavigateUrl = String.Format("{0}Themes.aspx", Groove.path_admin_host)
            hyp_stylesheeteditor.NavigateUrl = String.Format("{0}StyleSheetEditor.aspx", Groove.path_admin_host)
            hyp_managecontrols.NavigateUrl = String.Format("{0}UserControlsRegistry.aspx", Groove.path_admin_host)
            hyp_usercontroleditor.NavigateUrl = String.Format("{0}UserControlEditor.aspx", Groove.path_admin_host)
            hyp_tagsgroups.NavigateUrl = String.Format("{0}TagsGroups.aspx", Groove.path_admin_host)
            hyp_pingservices.NavigateUrl = String.Format("{0}PingServices.aspx", Groove.path_admin_host)

            opener.HRef = CoreSettings.Settings("FILEMANAGER_Link").Value




            ' hide the master page editor if there are no master pages to edit
            If Groove.admin_root.master_templates.Count = 0 Then li_masterpageeditor.Visible = False

            ' hide the stylesheet editor while figuring out the permission issues writting to the theme folder
            li_stylesheeteditor.Visible = False

            If Groove.admin_root IsNot Nothing Then


                lit_rootname.Text = Groove.admin_root.name
                hyp_dash.NavigateUrl = Groove.path_admin

                If Groove.admin_current_node IsNot Nothing Then
                    If Not Groove.admin_current_node.node_type = NodeType.Folder Then
                        Try
                            hyp_preview.NavigateUrl = Groove.admin_current_node.GetLink().ToString
                        Catch ex As Exception
                            hyp_preview.Visible = False
                        End Try
                    Else
                        hyp_preview.Visible = False
                    End If
                Else
                    Try
                        hyp_preview.NavigateUrl = Groove.admin_root.default_node.GetLink().ToString
                    Catch ex As Exception
                        hyp_preview.Visible = False
                    End Try
                End If

                ' setup the "switch to" list
                Dim roots As List(Of Root) = Root.GetRoots
                If roots.Count = 1 Then
                    switch_list.Visible = False
                Else
                    Dim binders As New List(Of Root)
                    Dim r As Root = Nothing
                    For Each r In roots
                        If Not r.root_id = Groove.admin_root.root_id Then binders.Add(r)
                    Next
                    rpt.DataSource = binders
                    rpt.DataBind()
                End If


                ' Hide the set as default if already default
                If Groove.admin_root.admin_initial_root Then lb_setdefault.Visible = False


                If Groove.admin_root.embedded_controls_layout IsNot Nothing Then
                    hyp_embeddedcontrols.NavigateUrl = String.Format("{0}EmbeddedControls.aspx", Groove.path_admin_host)
                End If
                hyp_layouts.NavigateUrl = String.Format("{0}Layouts.aspx", Groove.path_admin_host)
            Else
                hyp_preview.Visible = False
                If Not Request.Url.AbsolutePath.Contains("WebsiteInformation.aspx") Then Response.Redirect(hyp_websiteinfo.NavigateUrl)
                Me.Visible = False
            End If
        End Sub


        ''' <summary>
        ''' Setup the linkbuttons to "switch to" other roots
        ''' </summary>
        Protected Sub rpt_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles rpt.ItemDataBound
            Dim r As Root = CType(e.Item.DataItem, Root)
            If r IsNot Nothing Then

                Dim lb_root As LinkButton = CType(e.Item.FindControl("lb_root"), LinkButton)
                lb_root.Text = r.name
                lb_root.CommandArgument = r.root_id

            End If
        End Sub

        ''' <summary>
        ''' Sets the admin root_id stored in the session to the root_id passed in the commandarguement of 
        ''' the linkbutton.  This is not saved as the "admin_initial_root"
        ''' </summary>
        Protected Sub lb_root_Click(ByVal sender As Object, ByVal e As EventArgs)

            Dim root_id As Integer = CType(CType(sender, LinkButton).CommandArgument, Integer)
            Root.SaveAdminSiteRoot(Root.GetRoot(root_id))

            Groove.RefreshPage()

        End Sub


        Protected Sub addwiz_Click(ByVal sender As Object, ByVal e As EventArgs) Handles addwiz.Click

            Dim root_id As Integer = Root.InsertRoot("New website", "")
            ' Create the default publish record
            If root_id > 0 Then Boogaloo.Core.BLL.Publish.InsertPublish(root_id, DateTime.Now, "c")
            Root.SaveAdminSiteRoot(Root.GetRoot(root_id))

            Response.Redirect(hyp_websiteinfo.NavigateUrl)

        End Sub

        ''' <summary>
        ''' Sets the current admin root as the default admin root for each subsequent login
        ''' </summary>
        Protected Sub lb_setdefault_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lb_setdefault.Click

            Root.SetAdminInitialRoot(Groove.admin_root.root_id)
            Groove.RefreshPage()

        End Sub

    End Class

End Namespace